package com.project.psc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.psc.bean.OrderBean;
import com.project.psc.bean.ReturnBean;
import com.project.psc.bean.SupplyBean;
import com.project.psc.bean.SupplyGoodsBean;
import com.project.psc.mapper.IOrderMapper;
import com.project.psc.mapper.ISupplyGoodsMapper;
import com.project.psc.mapper.ISupplyMapper;
import com.project.psc.service.IOrderService;
import com.project.psc.util.ConvertStatus;
import com.project.psc.vo.SupplyVo;
import com.project.util.ConvertBeanUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private ISupplyGoodsMapper supplyGoodsMapper;
    @Autowired
    private ISupplyMapper supplyMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public IPage<OrderBean> cutByItem(Long supplierId, String pact, LocalDate startTime, LocalDate endTime, Integer pageNO) {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        qw.eq("fk_supplierId", supplierId);
        if (pact != null && pact.length() != 0) {
            qw.like("o_pact", pact);
        }
        if (endTime != null) {
            qw.le("o_time", endTime);
        }
        if (startTime != null) {
            qw.ge("o_time", startTime);
        }
        qw.orderByDesc("o_time");

        return orderMapper.selectPage(new Page<>(pageNO, 6), qw);
    }

    @Override
    public OrderBean findById(Long id) {
        OrderBean orderBean = orderMapper.selectById(id);
        QueryWrapper<SupplyGoodsBean> qw = new QueryWrapper<>();
        qw.eq("sg_pact", orderBean.getPact());
        List<SupplyGoodsBean> supplyGoodsList = supplyGoodsMapper.selectList(qw);
        orderBean.setSupplyGoodsList(supplyGoodsList);
        return orderBean;
    }

    @Override
    public List<SupplyVo> findSupplyList(String orderId) {

        List<SupplyBean> supplyList = supplyMapper.findSupplyList(orderId);
        List<SupplyVo> supplyVoList = ConvertBeanUtil.copyList(supplyList, SupplyVo.class, (m, n) -> {
            n.setStatus(ConvertStatus.orderStatus(m.getStatus()));
        });

        return supplyVoList;
    }

    @Override
    public boolean add(Long supplierId, String supplierName, String pact) {

        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        qw.eq("o_pact", pact);
        Long count = orderMapper.selectCount(qw);
        if (count != 0) {
            return false;
        }

        OrderBean order = new OrderBean();
        order.setPact(pact);
        order.setSupplierId(supplierId);
        order.setSupplierName(supplierName);
        String orderId = "" + supplierId + System.currentTimeMillis();
        order.setOrderId(orderId);
        QueryWrapper<SupplyGoodsBean> qw1 = new QueryWrapper<>();
        qw1.eq("sg_pact", pact);
        List<SupplyGoodsBean> list = supplyGoodsMapper.selectList(qw1);
        double sumPrice=0.0;
        for (int i = 0; i < list.size(); i++) {
            SupplyGoodsBean goodsBean = list.get(i);
            sumPrice+=goodsBean.getSum()* goodsBean.getPrice();
        }
        order.setSumPrice(sumPrice);
        orderMapper.insert(order);

        //发送合同编号给审核
        rabbitTemplate.convertAndSend("supplierOne","supplier.sendOrder",pact);

        return true;
    }
}
